-- 战斗UI-正下方"我"的血条栏

local StatusView = MClass("StatusView")

local proCfg = {"mana","energy","rage"}
local titleCfg = {"MP","EP","RP"}
function StatusView:ctor()
	--self.root 默认为根节点
	self.hpBar = self.root:findChild("bar_1")
	self.hpLabel = self.root:findChild("num_1")
    self._hpVisibleFlag = true

	self.manaBar = self.root:findChild("bar_2")
    self.energyBar = self.root:findChild("bar_3")
    self.rageBar = self.root:findChild("bar_4")
	self.manaLabel = self.root:findChild("num_2")
    self.hpTitleLable = self.root:findChild("tlt_1")
    self.manaTitleLable = self.root:findChild("tlt_2")

    self:init()

    -- 新手战不显示
    if not app:isBattleGuideComp() then
        self:setVisible(false)
    else
        self:setVisible(true)
    end
end

function StatusView:init()
    self.hpTitleLable:setString("")
    self.manaTitleLable:setString("")
	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.STATUS_MANA,handler(self,StatusView.setMana))
	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.STATUS_HP,handler(self,StatusView.setHp))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.STATUS_HP_HIDE,handler(self,StatusView._shouldHideAllHpUI))
end

function StatusView:setTargetId(id)
    self.targetId = id
end

function StatusView:calcPercent(min,max)
        local energy = min
        local energyMax = max

        if energy and energyMax then
            if self.manaLabel then
                self.manaLabel:setString(string.format("%d/%d",tonumber(energy),tonumber(energyMax)))
            end

            local precent = math.floor(100*energy/energyMax)
            local isAdd = true
            if self.mEnergyPrecent and self.mEnergyPrecent > precent then
                isAdd = false
            end

            self.mEnergyPrecent = precent

            if isAdd then
                if self.nowBar then
                    self.nowBar:setPercent(precent)
                    local ball = self.nowBar:getSlidBallRenderer()
                    if precent>=93 or precent<=7 then
                        ball:setVisible(false)
                    else
                        ball:setVisible(true)
                    end
                end
            end
        end
    end


function StatusView:setMana(mana,manaMax,rage,rageMax,newEnergy,newEnergyMax)
    self.manaBar:setVisible(false) --mana
    self.energyBar:setVisible(false) --energy
    self.rageBar:setVisible(false) --rage

    if manaMax and manaMax>0 then
        self.manaBar:setVisible(true)
        self.nowBar = self.manaBar
        self:calcPercent(mana/100,manaMax/100)
    end

    if rageMax and rageMax>0 then
        self.rageBar:setVisible(true)
        self.nowBar = self.rageBar
        self:calcPercent(rage/100,rageMax/100)
    end

    if newEnergyMax and newEnergyMax>0 then
        self.energyBar:setVisible(true)
        self.nowBar = self.energyBar
        self:calcPercent(newEnergy/100,newEnergyMax/100)
    end
end


function StatusView:setHp(hp,hpMax)
    if hp and hpMax then
        if self.hpLabel then
            self.hpLabel:setString(string.format("%d/%d",tonumber(hp),tonumber(hpMax)))
        end

        local precent = math.floor(100*hp/hpMax)
        local isAdd = true
        if self.mHpPrecent and self.mHpPrecent > precent then
            isAdd = false
        end

        self.mHpPrecent = precent
        self.mFakeHpPrecent = precent

        if isAdd then
            if self.hpBar then
                self.hpBar:setPercent(precent)
                local ball = self.hpBar:getSlidBallRenderer()
                if precent>=93 or precent<=7 then
                    ball:setVisible(false)
                else
                    ball:setVisible(true)
                end
            end
        end
    end
end

-- 隐藏血条显示
function StatusView:_shouldHideAllHpUI(flag)
    if self._hpVisibleFlag == flag then return end
    self._hpVisibleFlag = flag

    -- 隐藏血条时特效的显示
    if cc.isDead(self.hpHideEffectNode) then
        local params =
        {
            path = Res.HideHpEffect_1.dir,
            armatureName = Res.HideHpEffect_1.name,
            animationName = "",
            skeletonName = Res.HideHpEffect_1.name
        }
        local Armature = app:getClass("Armature")
        self.hpHideEffectNode = Armature.create(params)
        self.hpBar:getParent():addChild(self.hpHideEffectNode)
        self.hpHideEffectNode:setPosition(self.hpBar:getPosition())
    end
    self.hpHideEffectNode:setVisible(flag)
    if flag then
        self.hpHideEffectNode:play("effcreat", 1, function()
            self.hpHideEffectNode:play("effloop", 1, function()
                self.hpHideEffectNode:play("effdead", 1 )
            end )
        end )
    end
end

function StatusView:clearHideEffect()
    if not cc.isDead(self.hpHideEffectNode) then
        self.hpHideEffectNode:removeFromParent()
        self.hpHideEffectNode = nil
    end
end

return StatusView
